package com.budwk.app.sqfwV2.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONObject;
import com.budwk.app.exception.ServiceException;
import com.budwk.app.sqfw.models.Sqfw_WorkSummary;
import com.budwk.app.sqfw.services.SqfwWorkSummaryService;
import com.budwk.app.sqfwV2.dto.dulaying.DuLaYingPropagandaOrScreeningPdfDTO;
import com.budwk.app.sqfwV2.service.mine.WorkSummaryService;
import com.budwk.starter.common.openapi.annotation.ApiDefinition;
import com.budwk.starter.common.openapi.annotation.ApiOperation;
import com.budwk.starter.common.result.Result;
import com.budwk.starter.security.utils.SecurityUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.POST;
import org.nutz.mvc.annotation.Param;

import java.util.Date;
import java.util.List;

/**
 * @BelongsProject: dfxy-shequ
 * @Author: yangweimin
 * @CreateTime: 2024-06-18  15:32
 * @Description: TODO
 */
@IocBean
@At("/platform/sqfwV2/form/workSummary")
@ApiDefinition(tag = "个人工作总结")
@Slf4j
public class WorkSummaryController {
    @Inject
    private WorkSummaryService workSummaryService;
    @Inject
    private SqfwWorkSummaryService sqfwWorkSummaryService;

    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "巡查任务统计")
    @SaCheckLogin
    public Result<?> all() {
        workSummaryService.all();
        return Result.success();
    }

    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "巡查任务统计(时间格式 start=2024-06-20 end=2024-06-21)")
    //@SaCheckLogin
    public Result<?> everyday(@Param("start") String start, @Param("end") String end) {
        Date endDate = null;
        if (StringUtils.isNotBlank(end)) {
            try {
                DateUtil.parse(end, "yyyy-MM-dd"); // 验证日期格式。
            } catch (Exception e) {
                throw new ServiceException("日期格式不正确");
            }
            endDate = DateUtil.parse(end,"yyyy-MM-dd");
        }
        if (StringUtils.isNotBlank(start)) {
            try {
                DateUtil.parse(start, "yyyy-MM-dd"); // 验证日期格式。
            } catch (Exception e) {
                throw new ServiceException("日期格式不正确");
            }

            Date startDate = DateUtil.parse(start,"yyyy-MM-dd");

            if (endDate == null) {
                endDate = DateUtil.parse(DateUtil.format(new Date(), "yyyy-MM-dd"), "yyyy-MM-dd");
            }

            while (startDate.getTime() < endDate.getTime()) {
                Date s = startDate;
                Date e = DateUtil.offsetDay(startDate, 1);
                log.info("执行"+DateUtil.format(s, "yyyy-MM-dd")+"个人工作总结任务");
                workSummaryService.everyday(DateUtil.format(s, "yyyy-MM-dd"), DateUtil.format(e, "yyyy-MM-dd"));
                startDate = DateUtil.offsetDay(startDate, 1);
            }
        }
        return Result.success();
    }

    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "获取任务列表")
    @SaCheckLogin
    public Result<?> getByDate(@Param("fillDate") String fillDate, @Param("pageNo") int pageNo, @Param("pageSize") int pageSize) {
        return Result.data(sqfwWorkSummaryService.getByDate(SecurityUtil.getUserId(), fillDate, pageNo, pageSize));
    }

    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "获取工作总结")
    @SaCheckLogin
    public Result<?> getWorkSummaryByDate(@Param("fillDate") String fillDate, @Param("pageNo") int pageNo, @Param("pageSize") int pageSize) {
        return Result.data(sqfwWorkSummaryService.getWorkSummaryByDate(SecurityUtil.getUserId(), fillDate, pageNo, pageSize));
    }

    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "更新")
    @SaCheckLogin
    public Result<?> update(@Param("workSummaryList") List<Sqfw_WorkSummary> workSummaryList, @Param("idList") List<String> idList) {
        workSummaryService.update(workSummaryList, idList);
        return Result.success();
    }
}
